﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ForzaGT3.Data;
using ForzaGT3.Common.Configuration;
using ForzaGT3.Areas.Admin.Models;

namespace ForzaGT3.Areas.Admin.Controllers
{
    public partial class SeriesController : Controller
    {
        //
        // GET: /Admin/Series/

        public virtual ActionResult Index()
        {
            // Get paging values from the query string
            int pageNum = CustomGrid.QueryParams.getPageNumber("data", Request.QueryString);
            int pageSize = CustomGrid.QueryParams.getPageSize("data", Request.QueryString);

            // Get sorting values from query string
            string sort = CustomGrid.QueryParams.getSortField("data", Request.QueryString, new Series(), "CreatedDate");
            string stype = CustomGrid.QueryParams.getSortType("data", Request.QueryString, "desc");

            // Get filter where conditions and values from the query string            
            List<object> whereValues = new List<object>();
            string whereConditions = "";
            whereConditions += CustomGrid.QueryParams.getLINQQueryParam("data", "Title", Request.QueryString, whereValues);
            whereConditions += CustomGrid.QueryParams.getLINQQueryParam("data", "QualifyingRaceDate", Request.QueryString, whereValues);
            whereConditions += CustomGrid.QueryParams.getLINQQueryParam("data", "ChampionshipRaceDate", Request.QueryString, whereValues);

            // Get data from database
            int totalRecords;
            IQueryable<Series> data = Series.AdminGetAll(whereConditions, whereValues, sort, stype, pageNum, pageSize, out totalRecords);

            // Count the total number of records
            ViewBag.totalRecords = totalRecords;
            ViewBag.CurrentPageName = "Series";

            return View(data.ToList());
        }

        public virtual ActionResult Add()
        {
            return View();
        }

        [HttpPost]
        public virtual ActionResult Add(SeriesModel model)
        {
            
            if (ModelState.IsValid)
            {
                Series data = new Series();
                data.SeriesId = Guid.NewGuid();
                data.ShortTitle = model.ShortTitle;
                data.Title = model.Title;
                data.StartDate = model.StartDate;
                data.FinishDate = model.FinishDate;
                data.QualifyingRaceDate = model.QualifyingRaceDate;
                data.ChampionshipRaceDate = model.ChampionshipRaceDate;
                data.Remarks = model.Remarks;

                data.RemarksEng = model.RemarksEnglish;

                data.CircuitId = model.CircuitId;
                
                if (model.BannerImage != null)
                {
                    model.BannerImage.SaveAs(Server.MapPath("~" + SiteSettings.SeriesImageFolder) + model.BannerImage.FileName);
                    data.BannerUrl = SiteSettings.SeriesImageFolder + model.BannerImage.FileName;
                }

                data.CreatedBy = HttpContext.User.Identity.Name;
                data.CreatedDate = DateTime.Now;
                data.Insert();
                return RedirectToAction(MVC.Admin.Series.Index());
            }
            return View(model);
        }

        public virtual ActionResult Edit(Guid Id)
        {
            Series data = Series.GetById(Id);
            if(data!=null)
            {
                SeriesModel model = new SeriesModel();
                model.Id = data.SeriesId;
                model.Title = data.Title;
                model.ShortTitle = data.ShortTitle;
                model.StartDate = data.StartDate;
                model.FinishDate = data.FinishDate;
                model.QualifyingRaceDate = data.QualifyingRaceDate;
                model.ChampionshipRaceDate = data.ChampionshipRaceDate;
                model.Remarks = data.Remarks;

                model.RemarksEnglish = data.RemarksEng;

                model.BannerUrl = data.BannerUrl;
                model.CircuitId = data.CircuitId;

                ViewData.Model = model;
            }
            return View();
        }

        [HttpPost]
        public virtual ActionResult Edit(SeriesModel model)
        {
            if (ModelState.IsValid)
            {
                Series data = Series.GetById(model.Id);
                if (data != null)
                {
                    data.ShortTitle = model.ShortTitle;
                    data.Title = model.Title;
                    data.StartDate = model.StartDate;
                    data.FinishDate = model.FinishDate;
                    data.QualifyingRaceDate = model.QualifyingRaceDate;
                    data.ChampionshipRaceDate = model.ChampionshipRaceDate;
                    data.Remarks = model.Remarks;

                    data.RemarksEng = model.RemarksEnglish;

                    data.CircuitId = model.CircuitId;

                    if (model.BannerImage != null)
                    {
                        model.BannerImage.SaveAs(Server.MapPath("~" + SiteSettings.SeriesImageFolder) + model.BannerImage.FileName);
                        data.BannerUrl = SiteSettings.SeriesImageFolder + model.BannerImage.FileName;
                    }

                    data.ModifiedDate = DateTime.Now;
                    data.Update();
                    return RedirectToAction(MVC.Admin.Series.Index());
                }
            }

            return View(model);
        }

        public virtual ActionResult Delete(Guid Id)
        {
            Series data = Series.GetById(Id);
            if (data != null)
            {
                data.IsDeleted = true;
                data.Update();
            }

            return RedirectToAction(MVC.Admin.Series.Index());
        }
    }
}
